package com.uchedao.vehicledetection.network;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.google.gson.JsonObject;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.config.AppInfoManager;
import com.uchedao.vehicledetection.config.UserInfoManager;

import java.util.HashMap;
import java.util.Map;

/**
 * 实例化网络请求
 * Created by zhengxizhen on 15/8/19.
 */
public class HttpRequest {

    public final static int GET = Request.Method.GET;
    public final static int POST = Request.Method.POST;

    /**
     * 实例化网络请求
     *
     * @param mothod        com.android.volley.Request.Method.GET / com.android.volley.Request.Method.POST
     * @param action        请求模块
     * @param params        请求参数
     * @param listener      正确回调
     * @param errorListener 错误回调
     */
    public static Request getRequest(int mothod, String action
            , Map<String, String> params
            , Response.Listener<JsonObject> listener
            , Response.ErrorListener errorListener, boolean isLogin) {
        if (params == null) {
            params = new HashMap<String, String>();
        }
        // 添加统一参数
        params.put("_token", AppInfoManager.getToken());
        if (isLogin) {
            params.put("_passport", UserInfoManager.getPASS_PORT());
        }
        Request request;
        switch (mothod) {
            case GET:
                StringBuilder url = new StringBuilder();
                url.append(action);
                boolean isEnd = false;
                for (String key : params.keySet()) {
                    if (isEnd) {
                        url.append("&");
                    } else {
                        url.append("?");
                        isEnd = true;
                    }
                    url.append(key);
                    url.append("=");
                    url.append(params.get(key));
                }
                // 实例化GET请求
                request = new GetUrlRequest(url.toString(), listener, errorListener);
                break;
            case POST:
                // 实例化POST请求
                request = new PostRequest(params, Api.Net.HOST + action, listener, errorListener);
                break;
            default:
                request = null;
                break;
        }
        //设置超时时间 10 ＊ 1000
        request.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 1, 1.0f));
        return request;
    }

    /**
     * 实例化网络请求
     *
     * @param mothod        com.android.volley.Request.Method.GET / com.android.volley.Request.Method.POST
     * @param action        请求模块
     * @param params        请求参数
     * @param listener      正确回调
     * @param errorListener 错误回调
     */
    public static Request getRequest(int mothod, String action
            , Map<String, String> params
            , Response.Listener<JsonObject> listener
            , Response.ErrorListener errorListener) {
        return getRequest(mothod, action, params, listener, errorListener, true);
    }

}
